/**
 * 依赖全局变量 InfoGlobal
 * Created by shopnc on 2015/11/26.
 */
//定义表格
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'id',
        title: '编号',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastQuery: false
    },
    {
        id: 'name',
        title: '品种名称',
        type: 'string',
        headerClass: 'text-left width-150',
        columnClass: 'text-left width-150',
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'code',
        type: 'string',
        title: '品种代码',
        headerClass: 'text-left width-100',
        columnClass: 'text-left width-100',
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'type',
        title: '类型',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.commoType.T1,
            2: $lang.commoType.T2
        }
    },
    {
        id: 'icon',
        type: 'string',
        title: '品种图标',
        headerClass: 'text-left width-100',
        columnClass: 'text-left width-100',
        fastQuery: false
    },
    {
        id: 'sortNo',
        title: '排序',
        type: 'int',
        headerClass: 'text-left width-100',
        columnClass: 'text-left width-100',
        hideQuery:false
    },
    {
        id: 'status',
        title: '状态',
        type: 'int',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.commoStatus.T1,
            2: $lang.commoStatus.T2
        }
    },
    {
        id: 'decription',
        type: 'string',
        title: '品种描述',
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-400',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html="",
                edit="",
                state = "";
            if ($("#commoEdit") && $("#commoEdit").val() == 1) {
                edit = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }

            if ($("#commoStatus") && $("#commoStatus").val() == 1) {
                if(record.status == 1){
                    state = "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.stateInfo(" + record.id + ",\"" + 2+ "\",\"" + record.name + "\")'><i class='fa fa-ban'></i>&nbsp;禁用&nbsp;</a>";
                }else{
                    state = "<a href='javascript:;' class='btn btn-info btn-sm m-r-10' onclick='information.stateInfo(" + record.id + ",\"" + 1+ "\",\"" + record.name + "\")'><i class='fa fa-plus'></i>&nbsp;启用&nbsp;</a>";
                }

            }
            html = edit  + state;
            return html;
        }
    }
];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/commo/json/list',
    exportFileName: '品种列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id","delFlag"], Timestamp: ["createTime"]},
    onGridComplete: function (grid) {
    }
};
var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['asc_type','asc_sort_no','asc_id'];

grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
/**
 * 公告列表所使用的js
 */
var information = function () {

    /**
     * 刷新addmodal 上的图片
     */
    function _refreshImage() {
        $(".viewImage").jqthumb({
            width: 180,
            height: 100,
            after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }


    //编辑器对象[添加]
    var _ueAdd;
    //编辑器对象[编辑]
    var _ueEdit;
    //删除URL
    var stateUrl = ncGlobal.adminRoot + "commo/json/state";

    /**
     * 删除
     */
    function _stateInfo(id,status, content) {
        var option='';
        if(status == "1"){
            option += '启用';
        }else{
            option += '禁用';
        }
        var tpl = '您选择对交易品种 <strong>' + content + '</strong>进行'+ option + '操作。<br/>您确定要进行该操作吗?'
        $.ncConfirm({
            url: stateUrl,
            data: {
                id: id,
                status:status
            },
            content: tpl,
            alertTitle: option+"操作"
        });
    }

    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $addForm.find('input[name="name"]').val("");
            $addForm.find('input[name="code"]').val("");
            $(".addType").bootstrapSwitch('state', true);
            $addForm.find('input[name="sortNo"]').val("");
            $('#addFormPicPath').val("");
            $('#addFormPicImg').attr('src', ncGlobal.imgRoot+"default_image.gif");
            $("#contentAdd").val("");
            $(".alert-danger").remove();
            $addForm.psly().reset();
            _refreshImage();
        }
    }

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });

        // //图片上传
        $("#addFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#addFormPicPath').val(data.result.data);
                    $('#addFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-新增
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        /**
         * 上传插件绑定
         */
        $("#editFormPic").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            send: function (e, data) {
                //进行图片格式验证
                var reg=/^image\/(gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG)$/;
                if(reg.test(data.files[0].type)){
                    if(data.files[0].size>2*1024*1024){
                        $.ncAlert({
                            closeButtonText: "关闭",
                            autoCloseTime: 3,
                            content: "图片格式超过2M请重新上传！"
                        });
                    }
                }else{
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: "请上传符合格式要求的图片！"
                    });
                    return;
                }
            },
            done: function (e, data) {
                if (data.result.code == 200) {
                    $('#editFormPicPath').val(data.result.data);
                    $('#editFormPicImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-编辑
                    _refreshImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });

        // 新增公告对话框
        $("#addForm").on("nc.formSubmit.success", function (e) {
            addInfo.initAddModal();
        })

        //添加微信事件时显示时
        $('#addModal').on('show.bs.modal', function(event) {
            addInfo.initAddModal();
        });

        // bycj [ 编辑对话框显示时调用 ]
        $('#editModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
                modal.find('input[name="id"]').val(gridData.id);
                modal.find('input[name="name"]').val(gridData.name);
                modal.find('input[name="code"]').val(gridData.code);
                if(gridData.type == 1){
                    $(".editType").bootstrapSwitch('state', true);
                }else{
                    $(".editType").bootstrapSwitch('state', false);
                }
                modal.find('input[name="sortNo"]').val(gridData.sortNo);
                $('#editFormPicPath').val(gridData.icon);
                $('#editFormPicImg').attr('src', ncGlobal.fileRoot + gridData.icon);
                $('#contentEdit').val(gridData.decription);
                //清除错误提示
                editForm.psly().reset();
                $(".alert-danger").remove()
                _refreshImage();
        });


    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        stateInfo: _stateInfo,
    }
}()
$(function () {
    grid.load();
    information.init();
})